package com.itany.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.itany.conf.MyWebSocketConfig;
import com.itany.order.service.IRoderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * Created by tyh on 2018/9/23.
 */
@Controller
@RequestMapping("/testorder")
public class TestOrderController {

    @Reference
    private IRoderService roderService;

    @Autowired
    private AlipayClient alipayClient;

    @RequestMapping("/createImg")
    public void createImg(HttpServletResponse response)throws Exception{
        response.setHeader("Pragma","No-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires",0);
        response.setContentType("image/jpeg");
        String qrCode = roderService.getQrCode();
        Map<EncodeHintType,Object> hint = new HashMap<EncodeHintType,Object>();
        hint.put(EncodeHintType.CHARACTER_SET,"UTF-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(qrCode, BarcodeFormat.QR_CODE,200,200,hint);
        MatrixToImageWriter.writeToStream(bitMatrix,"jpeg",response.getOutputStream());
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }


    @RequestMapping("/paySuccess")
    @ResponseBody
    public String paySuccess()throws Exception{
        System.out.println("==================paySuccess==========================");
        CopyOnWriteArraySet<MyWebSocketConfig> webSocketSet = MyWebSocketConfig.webSocketSet;
        for(MyWebSocketConfig cfg:webSocketSet){
                   if("1".equals(cfg.getUserId())){
                       cfg.getSession().getBasicRemote().sendText("1");
                   }
        }
        return "success";
    }

    @RequestMapping("/createPayPage")
    public void createPayPage(HttpServletResponse response){
        try {
               response.setContentType("text/html;charset=UTF-8");
               String no = System.currentTimeMillis()+"";
               AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            request.setBizContent("{\"out_trade_no\":\"" + no + "\"," + "\"total_amount\":\""
                    + "0.01" + "\"," + "\"subject\":\"" + "天下画集" + "\"," + "\"body\":\"" + "如有什么建议欢迎留言评论" + "\","
                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");


            AlipayTradePagePayResponse alipayTradePagePayResponse = alipayClient.pageExecute(request);
            response.getWriter().write(alipayTradePagePayResponse.getBody());

        }catch (Exception e){
            e.printStackTrace();
        }

    }



}
